﻿/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010

//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
function generateHtmlPage(strHtml) {
    var header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd\">";

    header += "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>";
    for (i = 0; i < document.styleSheets.length; i++) {
        header += "<link href=\"" + document.styleSheets[i].href;
        header += "\" rel=\"stylesheet\" type=\"text/css\" media =\"screen\"/>";
    }
    header += "</head>";
    strHtml = header + "<body>" + strHtml + "</body></html>";

    return formatToSendingHTML(strHtml);
}

function formatToSendingHTML(strHtml) {
    /*
    Replaces in the html code some characters, leaving the string in only one line,
    with the < replaced to a left pointing arrow (\u2190) and > replaced to the right
    pointing arrow (\u2192). This values are needed to then convert the string into
    html - readeble code.
    003C -> 2190
    003E -> 2192
    0022 -> \\\"
    \n -> ""
    \r -> ""
    &%13 -> ""
    &%10 -> ""
    */

    var r = /\u0022/g;
    r = /\n/g;
    strHtml = strHtml.replace(r, "");
    r = /\r/g;
    strHtml = strHtml.replace(r, "");
    r = /\u003C/g;
    strHtml = strHtml.replace(r, "\u2190");
    r = /\u003E/g;
    strHtml = strHtml.replace(r, "\u2192");
    r = /&#10;|&#13;/g;
    strHtml = strHtml.replace(r, "");
    return strHtml;
}

function getHtmlString(strhtml, height)//This should be called from the html pages to generate the object to send.
{
    return generateHtmlPage(strhtml);
}

//////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////// 

(function(){
    try {
        function _HTMLShotData(options){
            try {
                var url;
                var privateParameters = {
                    attributes:{
                        width:null
                        ,height:null
                        ,imageFormat:null
                        ,htmlString:null
                    }
                    ,events:{
                        onError:function(){}
                    }
                };

                function _initialize(options){
                    try {
                        url = window.location.href;

                        privateParameters = new PrivateParameters(options);
                        privateParameters.attributes.width = privateParameters.attributes.width || null;
                        privateParameters.attributes.height = privateParameters.attributes.height || null;
                        privateParameters.attributes.bitmapFormat = privateParameters.attributes.bitmapFormat || null;
                        privateParameters.attributes.htmlString = privateParameters.attributes.htmlString || null;
                    }
                    catch (Error) {privateParameters.events.onError(Error);}
                }

                function _setAttributes(newAttributes){
                    try {
                        for(var attrib in newAttributes)
                        {
                            if(privateParameters.attributes[attrib] != undefined)
                                privateParameters.attributes[attrib] = newAttributes[attrib];
                        }
                    }
                    catch (Error) {privateParameters.events.onError(Error);}
                }

                function _sendData(){
                    try {
                        var form = document.createElement('form');
                        form.action = privateParameters.URL;
                        form.method = 'post';
                        form.style.display = 'none';
                        document.documentElement.appendChild(form);

                        var inputHidden = document.createElement('input');
                        inputHidden.type = 'hidden';
                        inputHidden.name = 'htmlShotValue';
                        inputHidden.value = JSONEncode({URL:url
                                                        ,Width:privateParameters.attributes.width
                                                        ,Height:privateParameters.attributes.height
                                                        ,BitmapFormat:privateParameters.attributes.bitmapFormat
                                                        ,HTMLString:privateParameters.attributes.htmlString
                        });
                        form.appendChild(inputHidden);
                                                
                        form.submit();
                    }
                    catch (Error) {privateParameters.events.onError(Error);}
                    return null;
                }

                /*----------------*/
                /*-----Public-----*/
                /*----------------*/

                /*----------------------*/
                /*--------Methods-------*/
                /*----------------------*/
                this.setHTMLString=function(newHTMLString){
                    _setHTMLString(newHTMLString);
                }
                this.sendData=function(){
                    _sendData();
                }
                this.getAttributes = function(){
                    return privateParameters.attributes;
                }
                this.setAttributes = function(newAttributes){
                    _setAttributes(newAttributes);
                }
                
                /*-----------------------------------------------------------------------*/
                /*------------------------------Instance it------------------------------*/
                /*-----------------------------------------------------------------------*/
                _initialize(options);
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
            }
            catch (Error) { window.status = Error.Message; }
        }

        window.PdfWidget = function(instanceOptions){return new _PdfWidget(instanceOptions);};
    }
    catch (Error) { window.status = Error.Message; }    
})();